package com.syn.synold4s.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.core.content.ContextCompat;

import com.gaoyi.basemodule.baseAdapter.BaseAdapter;
import com.gaoyi.basemodule.baseAdapter.BaseViewHolder;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.WeatherDto;
import com.syn.synold4s.databinding.ItemWeatherBinding;

public class WeatherAdapter extends BaseAdapter<ItemWeatherBinding, WeatherDto> {

    private Drawable mTmp, mFs, mHr;

    public WeatherAdapter(Context context) {
        super(context);
    }

    @Override
    protected void setBindingVariable(ItemWeatherBinding binding, int position) {
    }

    @Override
    protected void onBindItem(ItemWeatherBinding binding, WeatherDto item, int position) {
        String val = item.getVal() == null ? "0" : item.getVal();
        if (item.getCode().equals("TMP")) {
            mTmp = initDrawable(mTmp, R.drawable.ic_temperature);
            binding.tvWeather.setCompoundDrawablesRelative(mTmp, null, null, null);
            binding.tvWeather.setText(String.format("%s%%", val));
        } else if (item.getCode().equals("FS")) {
            mFs = initDrawable(mFs, R.drawable.ic_air_velocity);
            binding.tvWeather.setCompoundDrawablesRelative(mFs, null, null, null);
            binding.tvWeather.setText(String.format("%skm/h", val));
        } else {
            mHr = initDrawable(mHr, R.drawable.ic_humidity_level);
            binding.tvWeather.setCompoundDrawablesRelative(mHr, null, null, null);
            binding.tvWeather.setText(String.format("%s%%", val));
        }
    }

    private Drawable initDrawable(Drawable drawable, int src) {
        if (drawable == null) {
            drawable = ContextCompat.getDrawable(mContext, src);
            if (drawable != null) {
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            }
        }
        return drawable;
    }

    @Override
    protected void onCreateView(ItemWeatherBinding binding, BaseViewHolder<ItemWeatherBinding> holder) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.item_weather;
    }
}
